﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Client
{
    /// <summary>
    /// 控制台帮助
    /// </summary>
    static class ConsoleUtils
    {
        /// <summary>
        /// 写入错误
        /// </summary>
        /// <param name="errorMessage">消息</param>
        public static void WriteError(string errorMessage)
        {
            var color = Console.ForegroundColor;
            if (errorMessage == null)
            {
                errorMessage = "";
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Error:" + errorMessage);
            Console.ForegroundColor = color;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        /// <param name="color"></param>
        public static void ActionWrite(Action action, ConsoleColor color)
        {
            if (action != null)
            {
                var oldColor = Console.ForegroundColor;
                try
                {
                    Console.ForegroundColor = color;
                    action();
                }
                catch (Exception err)
                {
                    WriteError(err.Message);
                }
                finally
                {
                    Console.ForegroundColor = oldColor;
                }
            }            
        }
    }
}
